import EditControl from "./EditControl";
import EditPan from "./EditPan";
import EditRotate from "./EditRotate";
import EditScale from "./EditScale";

export default class MapboxEdit {
	constructor(map, options) {
		this.map = map;
		this.updateFeatureLayerFunc = options.updateFeatureLayerFunc;

		this.mode = null;
		this.edit = null;

		this.changeMode("edit_control");
	}
	changeMode(mode) {
		if (this.mode === mode) return;

		this.mode = mode;
		if (this.edit) this.edit.destroy();

		switch (mode) {
			case "edit_control":
				this.edit = new EditControl(this.map);
				break;

			case "edit_pan":
				this.edit = new EditPan(this.map);
				break;

			case "edit_rotate":
				this.edit = new EditRotate(this.map);
				break;

			case "edit_scale":
				this.edit = new EditScale(this.map);
				break;

			default:
				this.edit = new EditControl(this.map);
				break;
		}

		this.bindEvents();
	}
	bindEvents() {
		this.edit.bindEvents(this.updateFeatureLayerFunc);
	}
	destroy() {
		if (!this.edit) return;
		this.edit.destroy();
		this.edit = null;
		this.mode = null;
	}
	updateEditSource(data) {
		this.edit.updateEditSource(data);
	}
}
